<template>
  <div class="voting-system">
    <h1>在线投票系统</h1>
    <h2>请为以下选项投票：</h2>
    <div v-for="option in options" :key="option.id" class="option">
      <input type="radio" :value="option.id" v-model="selectedOption" />
      <label>{{ option.name }}</label>
      <span class="vote-count">（{{ option.votes }}票）</span>
    </div>
    <button @click="castVote">投票</button>
    <div v-if="hasVoted" class="result">
      <h2>投票结果：</h2>
      <ul>
        <li v-for="option in options" :key="option.id">
          {{ option.name }}: {{ option.votes }}票
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';

export default {
  name: 'VotingSystem',
  setup() {
    // 投票选项
    const options = ref([
      { id: 1, name: '选项一', votes: 0 },
      { id: 2, name: '选项二', votes: 0 },
      { id: 3, name: '选项三', votes: 0 }
    ]);
    
    // 选中的选项
    const selectedOption = ref(null);
    // 是否已投票
    const hasVoted = ref(false);

    // 投票方法
    const castVote = () => {
      if (!selectedOption.value) {
        alert('请先选择一个选项！');
        return;
      }
      
      const option = options.value.find(opt => opt.id === selectedOption.value);
      if (option) {
        option.votes++;
        hasVoted.value = true;
      }
    };

    return {
      options,
      selectedOption,
      hasVoted,
      castVote
    };
  }
};
</script>

<style scoped>
.voting-system {
  max-width: 600px;
  margin: auto;
  text-align: center;
}

.option {
  display: flex;
  align-items: center;
}

.vote-count {
  margin-left: 10px;
}
</style>
